﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace MultiEnvironment.WebApplication1
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            Configuration = configuration;
            // An 2019-12-25 [ 根据环境变量来加载不同的配置文件 ]
            //Configuration = new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
            //    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            //    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
            //    .AddEnvironmentVariables()
            //    .Build();



        }


        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            /*
             ASP.NET Core 在应用启动时读取环境变量 ASPNETCORE_ENVIRONMENT，并将该值存储在 IWebHostEnvironment.EnvironmentName 中。
             ASPNETCORE_ENVIRONMENT 可设置为任意值，但框架提供三个值：
             Development
             Staging
             Production（默认值）
             */

            if (env.IsDevelopment())// An 2019-12-25 [ 开发环境 ]
            {
                app.UseDeveloperExceptionPage();
            }
            else if (env.IsStaging())// An 2019-12-25 [ 预演环境/测试环境/UAT环境/ ]
            {

            }
            else if (env.IsProduction())// An 2019-12-25 [ 默认值: 生产环境 ]
            {

            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            if (env.IsEnvironment("Staging_2"))
            {

            }


            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
